package umaine.ece471.cookbook;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;


public class RecipeLister extends ListActivity{
	String[] dataList;
	
	String[] splitter;
	@Override
	public void onCreate(Bundle savedInstanceState) {
	  super.onCreate(savedInstanceState);
	  this.setTitle("Cookbook - List of Returned Recipes");
	  Intent incomingIntent = getIntent();
	  dataList = incomingIntent.getStringArrayExtra("recipes");
	  String[] recipeNames = new String[dataList.length];
	  for(int i=0; i<dataList.length; i++){
		  splitter = dataList[i].split("_");
		  recipeNames[i]=splitter[0];
	  }
	  setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, recipeNames));

	  ListView lv = getListView();
	  lv.setTextFilterEnabled(true);

	  lv.setOnItemClickListener(new OnItemClickListener(){
	    public void onItemClick(AdapterView<?> parent, View view,
	        int position, long id) {

	    	Intent outgoingIntent;
	    	outgoingIntent = new Intent(RecipeLister.this, RecipeViewer.class);
			outgoingIntent.putExtra("dataString",dataList[position]);
			startActivity(outgoingIntent);
			
		}
	  });
	}
}
